package com.share.wechat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.IdRes;

import com.mob.MobSDK;
import com.share.wechat.bean.Share;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


/**
 * Created by yuhb
 */
public class OpenBuilder {
    private Activity activity;

    private PlatformActionListener platformActionListener;

    public static OpenBuilder with(PlatformActionListener mListener, Activity mCotext) {
        OpenBuilder builder = new OpenBuilder();
        builder.platformActionListener = mListener;
        builder.activity = mCotext;
        return builder;
    }


    public WechatOperator useWechat() {
        return new WechatOperator();
    }

    public WeiBoOperator useWeiBo() {
        return new WeiBoOperator();
    }


    public class WechatOperator {


        public void shareToWeiChatCircle(Share share) {

            Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
            Platform.ShareParams shareParams = new Platform.ShareParams();
//            shareParams.setFilePath(ResourcesManager.getInstace(MobSDK.getContext()).getFilePath());
            shareParams.setText(share.getContent());
            shareParams.setTitle(share.getTitle());
            shareParams.setUrl(share.getUrl());
            Bitmap bitmap = share.getThumbBitmap();
            if (bitmap == null) {
                bitmap = getShareBitmap(activity, share.getBitmapResID());
            }
//            shareParams.setImageData(bitmap);//share.getImageUrl()
            shareParams.setImageUrl(share.getImageUrl());
            shareParams.setShareType(Platform.SHARE_WEBPAGE);
            platform.setPlatformActionListener(platformActionListener);
            platform.share(shareParams);
            bitmap.recycle();

        }


        public void shareToWeiChat(Share share) {
            Platform platform = ShareSDK.getPlatform(Wechat.NAME);
            Platform.ShareParams shareParams = new Platform.ShareParams();
            shareParams.setText(share.getContent());
            shareParams.setTitle(share.getTitle());
            shareParams.setUrl(share.getUrl());
            Bitmap bitmap = share.getThumbBitmap();
            if (bitmap == null) {
                bitmap = getShareBitmap(activity, share.getBitmapResID());
            }
//            shareParams.setImageData(bitmap);
//            shareParams.setImagePath(share.getImageUrl());
            shareParams.setImageUrl(share.getImageUrl());
            shareParams.setShareType(Platform.SHARE_WEBPAGE);
            platform.setPlatformActionListener(platformActionListener);
            platform.share(shareParams);
            bitmap.recycle();
        }

        public void shareToQQ(Share share) {
            Bitmap bitmap = share.getThumbBitmap();
            if (bitmap == null) {
                bitmap = getShareBitmap(activity, share.getBitmapResID());
            }
            Platform.ShareParams sp = new Platform.ShareParams();
            sp.setTitle(share.getTitle());
            sp.setTitleUrl(share.getUrl());// 标题的超链接
            sp.setText(share.getContent());
            sp.setImageUrl(share.getImageUrl());
//            sp.setSite("发布分享的网站名称");
//            sp.setSiteUrl("发布分享网站的地址");
            Platform qq = ShareSDK.getPlatform(QQ.NAME);
// 执行图文分享
            qq.share(sp);
            bitmap.recycle();

        }

    }


    public class WeiBoOperator {
        public void shareToWeiBo(Share share) {
//            Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
//            Platform.ShareParams shareParams = new Platform.ShareParams();
//            shareParams.setText(share.getContent());
//            shareParams.setImageUrl(share.getImageUrl());
//            if (isValidClientSina("com.sina.weibo")) {
//                shareParams.setUrl(share.getUrl());
//            }
////            shareParams.setUrl(share.getUrl());
//            shareParams.setShareType(Platform.SHARE_IMAGE);
//            platform.setPlatformActionListener(platformActionListener);
//            platform.share(shareParams);

//            Platform.ShareParams sp = new Platform.ShareParams();
//            sp.setTitle(share.getTitle());
//            sp.setTitleUrl(share.getUrl());// 标题的超链接
//            sp.setText(share.getContent());
//            sp.setImageUrl(share.getImageUrl());
////            sp.setSite("发布分享的网站名称");
////            sp.setSiteUrl("发布分享网站的地址");
//            Platform qq = ShareSDK.getPlatform(SinaWeibo.NAME);
//            // 执行图文分享
//            qq.share(sp);


            Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
            Platform.ShareParams shareParams = new Platform.ShareParams();
            shareParams.setText(share.getContent());
            shareParams.setImageUrl(share.getImageUrl());
//            shareParams.setImagePath(share.getImageUrl());
//            shareParams.setImageArray(ResourcesManager.getInstace(MobSDK.getContext()).randomPic());
//            if(!DemoUtils.isValidClientSina("com.sina.weibo")){
//                shareParams.setUrl("http://www.mob.com");
//            }
            shareParams.setUrl("http://www.mob.com");
            shareParams.setShareType(Platform.SHARE_IMAGE);
//            shareParams.setLongitude(ResourcesManager.longitude);
//            shareParams.setLatitude(ResourcesManager.latitude);
            platform.setPlatformActionListener(platformActionListener);
            platform.share(shareParams);


        }

    }

    @SuppressLint("WrongConstant")
    public static boolean isValidClientSina(String akp) {
        PackageInfo pi;
        try {
            pi = MobSDK.getContext().getPackageManager().getPackageInfo(
                    akp, PackageManager.GET_RESOLVED_FILTER);
            return true;
        } catch (Throwable t) {
        }
        return false;
    }


    public static Bitmap getShareBitmap(Context context, @IdRes int resId) {
        return BitmapFactory.decodeResource(context.getResources(), resId);
    }


}
